package com.atguigu.system.controller;

import com.atguigu.common.result.Result;
import com.atguigu.model.system.SysRole;
import com.atguigu.model.vo.AssginRoleVo;
import com.atguigu.model.vo.SysRoleQueryVo;
import com.atguigu.system.exception.GuiguException;
import com.atguigu.system.service.SysRoleService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @Author： ekode
 * @Date： 2025/9/5 上午9:34
 * @Describe：用户角色1
 */
@Api(tags = "角色管理")
@RestController
@RequestMapping("/admin/system/sysRole")
public class SysRoleController {

    @Autowired
    private SysRoleService sysRoleService;

    @ApiOperation(value = "根据用户分配角色")
    @PostMapping("/doAssign")
    public Result doAssign(@RequestBody AssginRoleVo assginRoleVo) {
        sysRoleService.doAssign(assginRoleVo);
        return Result.ok();
    }

    @ApiOperation(value = "根据用户获取角色数据")
    @GetMapping("/toAssign/{userId}")
    public Result toAssign(@PathVariable String userId) {
        Map<String, Object> roleMap = sysRoleService.getRolesByUserId(userId);
        return Result.ok(roleMap);
    }

    @ApiOperation(value = "获取角色")
    @PostMapping("/get/{id}")
    public Result get(@PathVariable String id) {
        SysRole role = sysRoleService.getById(id);
        return Result.ok(role);
    }

    @ApiOperation(value = "新增角色")
    @PostMapping("/save")
    public Result save(@RequestBody SysRole role) {
       boolean isSuccess=sysRoleService.save(role);
       if (isSuccess){
           return Result.ok();
       }else {
           return Result.fail();
       }

    }

    @ApiOperation(value = "修改角色")
    @PutMapping("/update")
    public Result updateById(@RequestBody SysRole role) {
       boolean isSuccess= sysRoleService.updateById(role);
        if (isSuccess){
            return Result.ok();
        }else {
            return Result.fail();
        }

    }

    @ApiOperation(value = "删除角色")
    @DeleteMapping("/remove/{id}")
    public Result remove(@PathVariable String id) {
        sysRoleService.removeById(id);
        return Result.ok();
    }

    @ApiOperation(value = "根据id列表删除")
    @DeleteMapping("/batchRemove")
    public Result batchRemove(@RequestBody List<String> idList) {
        sysRoleService.removeByIds(idList);
        return Result.ok();
    }


    //查询
    @ApiOperation(value = "获取全部角色列表")
    @GetMapping("findAll")
    public Result<List<SysRole>>  findAllRole(){
        //添加异常测试
       /* //int a= 10/0;
         try {
             int a= 10/0;
         }catch (Exception e){
             throw new GuiguException(20001, "执行了自定义异常");
         }*/
        List<SysRole>list = sysRoleService.list();
        return Result.ok(list);
    }


    //删除
    @ApiOperation(value = "删除全部角色列表")
    @DeleteMapping("removeRole/{id}")
    public Result removeRole(@PathVariable String id){
        boolean isSucess = sysRoleService.removeById(id);

        if (isSucess){
            return Result.ok();
        }else {
            return Result.fail();
        }

    }

    @ApiOperation(value = "获取分页列表")
    @GetMapping("findPageQueryRole/{page}/{limit}")
    public Result findPageQueryRole(
            @ApiParam(name = "page", value = "当前页码", required = true)
            @PathVariable Long page,

            @ApiParam(name = "limit", value = "每页记录数", required = true)
            @PathVariable Long limit,

            @ApiParam(name = "roleQueryVo", value = "查询对象")
            SysRoleQueryVo roleQueryVo) {
        Page<SysRole> pageParam = new Page<>(page, limit);
        IPage<SysRole> pageModel = sysRoleService.selectPage(pageParam, roleQueryVo);

        System.out.println(pageModel);

        // 添加空值检查
        if (pageModel == null) {
            return Result.fail("查询失败");
        }
        return Result.ok(pageModel);
    }



}
